//
//  cardViewController.m
//  PhyloHunter
//
//  Created by Sean Bramhall on 2013-06-11.
//
//	Programmer(s): Sean Bramhall, Adam Laforge
//	Team Name: The Gigglers (Group 14)
//
//	Changes:
/*		June 21
		- Updated Arrays to have 30 cards
 
		June 20
		- Change the 'Back' button to Border Layout
		- Modified the card Fade Out Functionality
			- Now based off an array
			- Default is faded out, the function colours the thumbnail
		- Function that allows Powered-up (scanned/unlocked/coloured) cards to go Full-Screen
			- While others cannot go Full-Screen
			- Bool Segue: shouldPerformSegueWithIdentifier
		
		June 19
		- Replaced the old style looking cards, with the new style
 
		June 12
		- Changed the 'Back' button so that it now closes out the view, rather than leaving it open when returning to the main menu
		- Added a navigation bar at the top, which is black, transparent and has a built in back button and title
		- Added the section title, "Beaty Museum Deck"
		- Created arrays to hold the information of 10 cards
		- Modified the Scroll Bar on the right of the page (view), so that it would not go into the navigation bar at the top
		- Change the colour of the Description text to Cyan
		- Added the ability to go to Full-screen when you touch a card
			- And a button on that view to close out of full-screen
 
		June 11
		- Made some cards load in, with a temporary name and description label
		- Made the app able to fade out a single card
 */
//	Bugs:
/*		- Removed the Bug related to the 'Back' button
 */
//
//	Task:	Is the Card Collection View Controller for the 'Card Viewer'
//			Shows all the Cards in a single, scrollable View
//			One card per row, and has sections
//				Currently, only one section: Beaty Museum Deck

#import "cardViewController.h"
#import "cardCollectionViewCell.h"
#import "cardCollectionHeaderView.h"
#import "showCardViewController.h"

@interface cardViewController () {
	
	//Create arrays, for: Card Names, Card Descriptions, Card File Names, and whether the cards have been unlocked (powered-up)
    NSArray *cardLabelNames, *cardDescriptions, *cardFileNames;
    NSMutableArray *cardUnlocked;
}

@end

@implementation cardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	
	//Array holding the File Names of each card image
	cardFileNames = [[NSArray alloc] initWithObjects:@"00.jpg", @"01.jpg", @"02.jpg", @"03.jpg", @"04.jpg", @"05.jpg", @"06.jpg", @"07.jpg", @"08.jpg", @"09.jpg", @"10.jpg", @"11.jpg", @"12.jpg", @"13.jpg", @"14.jpg", @"15.jpg", @"16.jpg", @"17.jpg", @"18.jpg", @"19.jpg", @"20.jpg", @"21.jpg", @"22.jpg", @"23.jpg", @"24.jpg", @"25.jpg", @"26.jpg", @"27.jpg", @"28.jpg", @"29.jpg", @"30.jpg", nil];
	
	//Array that holds the name of each card
	cardLabelNames = [[NSArray alloc] initWithObjects:@"Home Card", @"Black Bear", @"Blue Whale", @"Clark's Nutcracker", @"Climate Change", @"Dragonfly", @"Earthworm", @"European Honey Bee", @"Giant Kelp", @"Habitat Loss", @"Himalayan BlackBerry", @"Human", @"Moss", @"Killer Whale", @"Marbled Murrelet", @"Mosquito", @"Moss Mite", @"Mycorrhizal Fungi", @"Oil Spill", @"Pacific Krill", @"PhytoPlankton", @"Purple Sea Urchin", @"Salal", @"Sea Otter", @"Sockeye Salmon", @"Species Protection", @"Steller Sea Lion", @"Three-Spined Stickleback", @"WhiteBark Pine", @"Wildfire", @"Zooplankton", nil];
	
	//Array of Short Descriptions of Each Card
	cardDescriptions = [[NSArray alloc] initWithObjects:
						@"This is a HOME card. The game starts with each player having a HOME card played on the table next to each other.",
						@"Play: Ursus americanus has a MOVE of 2.",
						@"Play: Balaenoptera musculus has a MOVE of 2 and requires a KRILL card for diet",
						@"Play: Nucifraga columbiana has a FLIGHT of 2.",
						@"Play: When playing this card, roll six sided die and consult below...",
						@"Play: Anisoptera has a FLIGHT of 2",
						@"Play: Lumbricus terrestris has a MOVE of 1",
						@"Play: Apis mellifera has a FLIGHT of 2 and is considered a POLLINATOR",
						@"Fact: Macrocystis pyrifera is one of the fastest-growing organisms on Earth",
						@"Play: Place this card on any SPECIES card and changes its habitat to URBAN permanently",
						@"Fact: Himalayan blackberry is native to Armenia and northern Iran, but is now widespread across most of the temperate world.",
						@"Play: Humans have a MOVE of 2. This card may only be played next to PLANT and ANIMAL SPECIES that a human would actually eat.",
						@"Fact: Like other mosses, Isothecium cristatum does not have flowers and seeds, but reproduces using spores and spore capsules.",
						@"Play: Killer whale has a MOVE of 2.",
						@"Play: Marbled murrelet has a FLIGHT of 2.",
						@"Play: Mosquito has a MOVE of 2 and may be played next to a HUMAN, BLACK BEAR, CLARK’S NUTCRACKER, or WHITEBARK PINE SPECIES card.",
						@"Play: Moss mite has a MOVE of 1.",
						@"Play: Mycorrhizal fungi must be played adjacent to a PLANT SPECIES.",
						@"Play: Place this card on any OCEAN or FRESHWATER based SPECIES card.",
						@"Play: Pacific krill has a MOVE of 1 and feeds (should be played adjacent to) PLANKTON SPECIES.",
						@"Play: Phytoplankton has a MOVE of 1 and is often eaten by KRILL SPECIES.",
						@"Play: Purple sea urchin has a MOVE of 1 and must be played adjacent to at least one OCEAN terrain.",
						@"Fact: Salal has edible berries and is also an important medicinal plant for Aboriginal peoples.",
						@"Play: Sea otter has a MOVE of 2.",
						@"Play: Sockeye salmon has a MOVE of 2 and can feed on ZOOPLANKTON SPECIES.",
						@"Play: You can play this card immediately after your opponent has played an EVENT card on one of your SPECIES.",
						@"Play: Steller sea lion has a MOVE of 2.",
						@"Play: Three-spined stickleback has a MOVE of 2 and can feed on ZOOPLANKTON SPECIES.",
						@"Fact: Whitebark pines may grow more than 20 metres in height, if conditions are favourable.",
						@"Play: Place this card on top of any SPECIES card on GRASSLAND or FOREST terrain.",
						@"Play: Zooplankton has a MOVE of 1 and can feed SOCKEYE SALMON and BLUE WHALE SPECIES cards.",
						nil];
	
	//Array that holds the Status of the Card on whether it is Unlocked (Powered-Up) or not
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"arrays.out"];
    cardUnlocked = [NSMutableArray arrayWithContentsOfFile:arrayPath];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//How many Cells does the View need? Return size of Array that holds the File Names
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	return [cardFileNames count];
}


//Build every cell: Card ThumbNail, Name, Short Description
//		- Set the visibility (Faded or Full-Colour) of the ThumbNail to show whether it is unlocked (meaning powered-up/scanned)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

	//Each Card has its own Cell (ThumbNail, Name of Card, Short Description about the card)
    static NSString *identifier = @"Cell";
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
	//Display the ThumbNail of the Card in the Cell
	// (Example: if the card was the Blue Whale, you would see a small thumbnail version of the Blue Whale card)
    UIImageView *cardImageView = (UIImageView *)[cell viewWithTag:100];
	cardImageView.image = [UIImage imageNamed:[cardFileNames objectAtIndex:indexPath.item]];
	
	
	//Display the Name of the Card in the Cell
	// (Example: next to the Blue Whale card thumbnail, it would say Blue Whale)
	UILabel *cardLabel = (UILabel *)[cell viewWithTag:5];
	cardLabel.text = [cardLabelNames objectAtIndex:indexPath.item];

	
	//If the Card is Unlocked (Powered-Up) present it in Full Colour
	// Thumbnails of cards are defaulted to be a gray, faded look.
	// Unlocked (powered-up/scanned) cards are displayed in Full Colour
	if ([[cardUnlocked objectAtIndex:indexPath.item] isEqualToString:@"Unlocked"]) {
		cardImageView.alpha=1.0f; //Full-colour
	} else {
		cardImageView.alpha=0.25f; //Faded
	}
	
	
	//Display a short description of the card in the Cell
	// To the right of the thumbnail, and below the name of the card
	// (Example, for the Blue Whale card, to the right of the Blue Whale Card Thumbnail would be the name Blue Whale and then right below it would be this description)
	UILabel *descriptiveCardText = (UILabel *)[cell viewWithTag:6];
	descriptiveCardText.text = [cardDescriptions objectAtIndex:indexPath.item];

	
	return cell;
}


//Creates the "Beaty Museum Deck" Header (can be used to create other deck headers, to split the viewer)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        cardCollectionHeaderView *cardViewerHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cardViewerHeader" forIndexPath:indexPath];
        NSString *title = [[NSString alloc]initWithFormat:@"Beaty Museum Deck"];
        cardViewerHeader.cardHeaderTitle.text = title;
        UIImage *headerImage = [UIImage imageNamed:@"header_banner.png"];
        cardViewerHeader.backgroundImage.image = headerImage;
        
        reusableview = cardViewerHeader;
    }
	
    return reusableview;
}

//Send Card information and Open Card in Full-Screen Mode if Unlocked (powered-up/scanned)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	
	if ([segue.identifier isEqualToString:@"showCardImage"]){
		NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
		NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
		showCardViewController *destViewController = segue.destinationViewController;
		destViewController.cardImageName = [cardFileNames objectAtIndex:indexPath.item];
		[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
	}
}

//Prevent Segue (Full-Screen Card View) from working if card isn't unlocked (powered-up/scanned)
- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
	
	NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
	NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
	
	//If the Card is Locked prevent Full Screen Mode
	if ([[cardUnlocked objectAtIndex:indexPath.item] isEqualToString:@"Locked"]) {
		return NO;
	} else {
		return YES;
	}
}

//Card Viewer Close Screen Button
- (IBAction)closeCardViewer:(id)sender {
    [self dismissViewControllerAnimated:YES completion:NULL];
}
@end
